#import "CoinGame.h"
#import <Foundation/NSString.h>
#import <Foundation/Foundation.h>
#import "coinGameViewController.h"

@implementation CoinGame

@synthesize timeLeft;
@synthesize score;
@synthesize playing;
@synthesize M;
@synthesize value;
@synthesize D;
@synthesize P;
@synthesize speed;
@synthesize life;
@synthesize paused;
@synthesize stoppage;
@synthesize delayAmount;
@synthesize goNext;

// set mode
-(void)setMode:(int)input{
	M=input%10;
	D=floor((input/10)%10);
	P=floor(input/999);
}

//display the mode
-(IBAction)checkMode{
	
	
	[displayField setText:[NSString stringWithFormat:@" Mode is %i %i %i  ", M, D,P]]; 
}

//when a penny button is press




-(IBAction)coin:(id)sender;
{
	if ((paused||stoppage)==false) //this is to keep coin from being press a 2second after the game and during pause
	{
		NSString *button = [sender titleForState:UIControlStateNormal];
		//add penny
		if ([button isEqualToString:@"Add Penny"])
		{ 
			
			[pDisplay setText:[NSString stringWithFormat:@"%ld", [current addPenny]]]; 
			
		}//code for minusing a penny
		else if ([button isEqualToString:@"Minus Penny" ])
		{
			
			[pDisplay setText:[NSString stringWithFormat:@"%ld", [current decPenny]]]; 
			
		}
		
		//add nickel
		if ([button isEqualToString:@"Add Nickel"])
		{ 
			
			[nDisplay setText:[NSString stringWithFormat:@"%ld", [current addNickel]]]; 
			
		}
		else if ([button isEqualToString:@"Minus Nickel" ])
		{
			//decrease nickel
			[nDisplay setText:[NSString stringWithFormat:@"%ld", [current decNickel]]]; 
			
		}
		
		
		if ([button isEqualToString:@"Add Dime"])
		{ 
			
			[dDisplay setText:[NSString stringWithFormat:@"%ld", [current addDime]]]; 
			
		}
		else if ([button isEqualToString:@"Minus Dime" ])
		{
			
			[dDisplay setText:[NSString stringWithFormat:@"%ld", [current decDime]]]; 
			
		}
		
		
		if ([button isEqualToString:@"Add Quarter"])
		{ 
			
			[qDisplay setText:[NSString stringWithFormat:@"%ld", [current addQuater]]]; 
			
		}
		else if ([button isEqualToString:@"Minus Quarter" ])
		{
			
			[qDisplay setText:[NSString stringWithFormat:@"%ld", [current decQuater]]]; 
			
		}
		
		
		if ([button isEqualToString:@"Add Loonie"])
		{ 
			
			[lDisplay setText:[NSString stringWithFormat:@"%ld", [current addLoonie]]]; 
			
		}
		else if ([button isEqualToString:@"Minus Loonie" ])
		{
			
			[lDisplay setText:[NSString stringWithFormat:@"%ld", [current decLoonie]]]; 
			
		}
		
		
		if ([button isEqualToString:@"Add Toonie"])
		{ 
			
			[tDisplay setText:[NSString stringWithFormat:@"%ld", [current addToonie]]]; 
			
		}
		else if ([button isEqualToString:@"Minus Toonie" ])
		{
			
			[tDisplay setText:[NSString stringWithFormat:@"%ld", [current decToonie]]]; 
			
		}
	}
}
//get next question
-(void)nextQ:(int)level;
{
	float quetsion;
	int hardness; //hardness determine the question that going to be asked
	
	
	//update coins display
	[pDisplay setText:[NSString stringWithFormat:@"0"]]; 
	[nDisplay setText:[NSString stringWithFormat:@"0"]]; 
	[dDisplay setText:[NSString stringWithFormat:@"0"]]; 
	[qDisplay setText:[NSString stringWithFormat:@"0"]]; 
	[lDisplay setText:[NSString stringWithFormat:@"0"]]; 
	[tDisplay setText:[NSString stringWithFormat:@"0"]]; 
	
	if(M==1) //for 60 second race mode 
	{
		if(P==1) //if p is one that mean the diffcuitly doesn't increase
		{
			if(D!=0) //diffcuilty is one higher then the D value
					//beside for D=0 which set diffcuilty to 0
			{
				hardness=D+1;
			}
			else
			{
				hardness=D;
			}
		}
		else  //if the game is set to get harder
		{
			if (level<6) //after 6 question increase hardness
			{			//easy mode only go up one level, normal mode go up 2 and hard mode go up 1
				hardness=D;
				if(D==1)
				{
					hardness=2;
				}
				
			}
			else
			{
				hardness=D+1;
				if(D==1)
				{
					hardness=3;
				}
			}
			
		}
	}
	else if(M==2) //for 10 question mode
	{
		
		if(P==1) //if diffcuilty doesn't increase use this to assign the diffcuilty of questions
		{
			if(D!=0)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D;
			}
		}
		else //if diffcuilty increase then it increase at question 4 and 8
		{
			if(level<4)
			{
				hardness=D;	
			}
			else if (level<8)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D+2;
			}
		}
	}
	else if(M==3) //for sudden death mode
	{
		
		if(P==1)   //use this to assign diffcuilty if it not set t increaes
		{
			if(D!=0)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D;
			}
		}
		else      //for this mode diffcuilty is based on score, so hardness go up when their score are at 100 and 200
		{			//diffcuilty will increase at about the 6 and 12 question depending on how fast they answer questions
			if(level<100)
			{
				hardness=D;	
			}
			else if (level<200)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D+2;
			}
		}
	}
	else if(M==4)
	{    //for survial mode
		
		if(P==1)
		{
			if(D!=0)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D;
			}
		}
		else   //diffculty increase at question 8 and 16 and can reach 2 above the starting level
		{
			if(level<8)
			{
				hardness=D;	
			}
			else if (level<16)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D+2;
			}
		}	
	}else
	{	//the if case is for 60 second cashier mode
		
		if(P==1)//if diffcuilty not set to incrase
		{
			if(D!=0)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D;
			}
		}
		else
		{   //if diffcuilty set to increase, incrase it at the 8 and 16 question
			if(level<8)
			{
				hardness=D;	
			}
			else if (level<16)
			{
				hardness=D+1;
			}
			else
			{
				hardness=D+2;
			}
		}	
	}	
	
	
	
	
	if(M!=5)  //for all modes beside 60 second chasier mode the questions are from easyValue, easier Value, randomValue, and hardValue functions
	{		//the valuse is kept incase the game is paused
		if(hardness==0)
		{
			value=[current easierValue];
			[displayField setText:[NSString stringWithFormat:@"Make %.2f out of coins", value]];	
		}
		else if(hardness==1)
		{
			value=[current easyValue];
		[displayField setText:[NSString stringWithFormat:@"Make %.2f out of coins", value]];			}
		else if(hardness==2)
		{
			value=[current randomValue];
		[displayField setText:[NSString stringWithFormat:@"Make %.2f out of coins", value]];			}
		else
		{
			value=[current hardValue];
		[displayField setText:[NSString stringWithFormat:@"Make %.2f out of coins", value]];			}
	}
	else
	{	//for 60 second chaseir mode, question are gotten from easiervaluse, easyValue and randomValuse functions
		//hardValue is taken out because it might be too challageing for kids and randomValue function give hard questions sometimes as well
		
		if(hardness==0)
		{
			quetsion =[current easierValue];
		}
		else if(hardness==1)
		{
			quetsion =[current easyValue];
		}
		else
		{
			quetsion =[current randomValue];
		}
		
		value=quetsion;//save question in case of game pause
		
		if(quetsion>5)  //if value is more then 5 dollar use 10 dollar for top
		{
			[
			 displayField setText:[NSString stringWithFormat:@"A peron give you $10.00"]];
			quetsion=10-quetsion;
			[displayField2 setText:[NSString stringWithFormat:@"but they own you $%.2f", quetsion]];
		}
		else//else the value is from 5
		{
			[displayField setText:[NSString stringWithFormat:@"A peron give you $5.00"]];
			quetsion=5-quetsion;
			[displayField2 setText:[NSString stringWithFormat:@"but they own you $%.2f", quetsion]];
			
		}	
	}
	
}		








//class to check if answer is correct
-(IBAction)getAnswer:(id)sender;
{
	if ((paused||stoppage)==false)
	{
		float answer;
		answer=[current total]; //get answer from coinCount class
		//however if playing ==false that mean the game didn't start so show error
		if(playing==false)
		{
			[displayField setText:[NSString stringWithFormat:@"Please press [Question] first "]];
			[displayField2 setText:[NSString stringWithFormat:@"To start the game "]];
			
		}
		else if (M==1)//if m=1 that mean the type of game is answer as many question as you can in 60 sec
		{
			if (answer==-1) // if the answer is correct
			{
				score++; //increase score
				[displayField2 setText:[NSString stringWithFormat:@"Correct, your score is %ld", score]];//display socre
				
				//get a quesiton based on how many question the player got right so far
				[self nextQ:score];		
				
			}
			else
			{
				[displayField2 setText:[NSString stringWithFormat:@"Your answer is incorrect, total %.2f",answer]]; //display what value the player enter
				
				//get a quesiton based on how many question the player got right so far
				[self nextQ:score];		
			}
		}
		else if (M==2) //asnwer 10 question as fast as the player can
		{
			if(answer==-1)
			{
				score++; //keep count of how many questino they got correct
				[displayField2 setText:[NSString stringWithFormat:@"Correct, you have answered %i questions", score]];
				
				if(score==10) //this is for when they get 10 qestion right, it should their final time
				{
					playing=false;
					[displayField setText:[NSString stringWithFormat:@"Congratulations"]];
					[displayField2 setText:[NSString stringWithFormat:@"You answered 10 questions in %.2f", timeLeft]];
					[self delaying:2];
				}
				else
				{				//get a quesiton based on how many question the player got right so far
					[self nextQ:score];			
				}
				
			}
			else //else is for when they don't get the questino right
			{
				[displayField2 setText:[NSString stringWithFormat:@"Your answer is incorrect, total %.2f",answer]];
				//get a quesiton based on how many question the player got right so far
				
				[self nextQ:score];		
			}
		}
		else if (M==3)
		{
			if (answer==-1)
			{
				score=score+timeLeft+10;
				[displayField2 setText:[NSString stringWithFormat:@"Correct, your score is %ld", score]];
				timeLeft=20;
				[timeDisplay setText:[NSString stringWithFormat:@"20"]];
				[self timeControl:2];
				
				//get a quesiton based on how many question the player got right so far
				
				[self nextQ:score];	
				
			}
			else
			{		
				playing=false;
				[displayField setText:[NSString stringWithFormat:@"The correct answer is %.2f",[ current getquestion]]];
				[displayField2 setText:[NSString stringWithFormat:@"Your final score is %i",score]];
				[self delaying:2];
			}
		}
		else if (M==4) // this is for survial mode
		{
			if (answer==-1) //if they get the quetstion right
			{
				score++; //increase score
				
				if(score%4==0) // this is increaseig the player lives for every 4 question they get right
				{
					life++;
				}
				
				[displayField2 setText:[NSString stringWithFormat:@"You have %ld life remaining", life]]; //display lives left
				
				//update time
				timeLeft=20;
				[timeDisplay setText:[NSString stringWithFormat:@"20"]];
				[self timeControl:2];
				
				//get a quesiton based on how many question the player got right so far
				[self nextQ:score];	
				
			}
			else //this is for if the player get the questino wrong
			{	
				life--; //decrease their lives
				if (life==0) //if they are out of lives the game is over
				{
					//[self timeControl:0];
					
					
					//end game and show their final score and the correct answer
					playing=false;
					[displayField setText:[NSString stringWithFormat:@"Your answer is incorrect, total %.2f",answer]];
					[displayField2 setText:[NSString stringWithFormat:@"Your final score is %i",score]];
					[self delaying:2];
				}
				else 
				{
					[displayField2 setText:[NSString stringWithFormat:@"You have %ld life remaining", life]];
					timeLeft=20;
					[timeDisplay setText:[NSString stringWithFormat:@"20"]];
					
					
					//get a quesiton based on how many question the player got right so far
					[self nextQ:score];	
					
					
				}
			}
		}
		else if (M==5)
		{
			if (answer==-1)
			{
				score++;
				//[displayField setText:[NSString stringWithFormat:@"Your answer is correct"]];
				[displayField setText:[NSString stringWithFormat:@"Your answer is correct"]];
				[displayField2 setText:[NSString stringWithFormat:@"Your score is %ld", score]];
				
				[self delaying:-1];
				
				
			}
			else
			{
				[displayField2 setText:[NSString stringWithFormat:@"Your answer is incorrect, total %.2f",answer]];
				[displayField setText:[NSString stringWithFormat:@"The correct answer is %.2f", value]];
				[self delaying:-1];
				
				
			}
		}
	}
}		
//use to delay the game
//this is use so player don't press answer by mistake after the game is over
//if they do press it, it would cover up their score so they can't see
//what they got

-(void)delaying:(int)goDelayFor;
{
	
	if(goDelayFor>0) //if delay is more then 0 it mean the lay is set
	{
		stoppage=true;
		delayAmount=goDelayFor;  //set variable for time to delay
		//start timer for when to stop delay
		myTimer2 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(waiting) userInfo:nil repeats:YES];
	}
	if(goDelayFor==-1)
	{    //if goDelayFor is -1 then mean it chaiser mode and you are delaying for between questions
		
		stoppage=true;

		delayAmount=2;
		myTimer2 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(waiting) userInfo:nil repeats:YES];
		goNext=true;
		[self timeControl:0];
		
	}
	if(goDelayFor==-5)  //if goDelayFor is -5 that mean end the delay
	{
		stoppage=false;
		[myTimer2 invalidate];
		if(goNext==true)   //this is to dispaly the next quesiton for mode 5
		{
			goNext=false;
			[self nextQ:score];
			[self timeControl:3];
			
			
		}
		
	}
	
}

-(void)waiting    //this is used for the delay, it count down and tell the delaying function when to go again
{
	delayAmount--;
	if(delayAmount==0)
	{
		[self delaying:-5];
	}
}



-(void)timeControl:(int)startStop //class used to control time
{									//it can stop, start, and speed up the timer
	if(startStop==1) // if input is 1, start timer
	{
		if(M==2) //if it at mode 1 set the time to count in .1 second interval, this way the final socre is more accurate
		{
			[myTimer invalidate];
			speed=.1;
			myTimer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(showTime) userInfo:nil repeats:YES];
			return;	
		}
		
		//if mode isn't at 2 then set timer for 1 second interval
		[myTimer invalidate];
		myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES];
		
	}
	if(startStop==0) //if input is 0, stop time. this if statment will be used for pauseing the game
	{
		[myTimer invalidate];
	}
	if(startStop==2) // if statemtnt for increaseing time. currently this is used to increase game speed by 10%
	{
		[myTimer invalidate];
		speed=speed*.9;
		myTimer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(showTime) userInfo:nil repeats:YES];
		
	}
	if(startStop==3) // if statemtnt for increaseing time. currently this is used to increase game speed by 10%
	{
		myTimer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(showTime) userInfo:nil repeats:YES];
		
	}
}

-(IBAction)pause:(id)sender;  //this functions is used to pasuse the game
{
	
	int question; //this is for mode 5
		
	if(stoppage==false){//pausing during stoppage freeze game, so don't allow it
		if(paused==false)
		{//if the game isn't pause pause it
		
		
			//display this
			[displayField setText:[NSString stringWithFormat:@"Game is currently paused"]];
			[displayField2 setText:[NSString stringWithFormat:@"Press [Pause] to contiune"]];
		
			[self timeControl:0];  //stop timer
			paused=true; //set pause to be true to disable buttons
		}
		else//else is for when you are unpausing the game
		{
			[self timeControl:3]; // resume timer
		paused=false; //set pasue to be false to enable buttons
		
			//display the current questions
			[displayField setText:[NSString stringWithFormat:@"Make %.2f out of coins", value]];
		
		
			//this is for what to display after the game is unpause
			if (M==1)//if m=1 that mean the type of game is answer as many question as you can in 60 sec
			{
			
				[displayField2 setText:[NSString stringWithFormat:@"Correct, your score is %ld", score]];//display socre
			}
			else if (M==2) //asnwer 10 question as fast as the player can
			{
				[displayField2 setText:[NSString stringWithFormat:@"Correct, you have answered %i questions", score]];
			}	
			else if (M==3)//mode 3
			{
			
				[displayField2 setText:[NSString stringWithFormat:@"Correct, your score is %ld", score]];
			}
			else if (M==4) // this is for survial mode
			{
				[displayField2 setText:[NSString stringWithFormat:@"You have %ld life remaining", life]]; //display lives left
			}
			else//for mode 5
			{
				if(value>5)  //if value is more then 5 dollar use 10 dollar for top
				{
					[displayField setText:[NSString stringWithFormat:@"A peron give you $10.00"]];
					question=10-value;
					[displayField2 setText:[NSString stringWithFormat:@"but they own you $%.2f", question]];
				}
			else//else the value is from 5
			{
				[displayField setText:[NSString stringWithFormat:@"A peron give you $5.00"]];
				question=5-value;
				[displayField2 setText:[NSString stringWithFormat:@"but they own you $%.2f", question]];
				
			}		
		}
		
	}
	
	}
}






-(IBAction)starGame:(id)sender;  //code used to start new game
{
	
	if ((paused||stoppage)==false)
	{
		
		if(playing==true) //this was used before to stop time when a new is started but temportally being commented out
		{
			//[self timeControl:0	];
		}
		//update all displays and display new question
		[pDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[nDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[dDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[qDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[lDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[tDisplay setText:[NSString stringWithFormat:@"0"]]; 
		[displayField2 setText:[NSString stringWithFormat:@""]];
		[timeDisplay setText:[NSString stringWithFormat:@"0"]];
		score=0;
		speed=1;
		playing=true; //set playing to be true
		if (M==1) //if mode is one set the timeleft to be 60 and start the timer
		{
			[self nextQ:score];
			timeLeft=60;
			[timeDisplay setText:[NSString stringWithFormat:@"60"]];
			[self timeControl:1	];
		}
		if(M==2) //if mode is 2, set the timeleft and start timer
		{
			[self nextQ:score];
			timeLeft=0;
			[self timeControl:1	];
		}		
		if(M==3) //if mode is m set time to 20 and start timer
		{
			[self nextQ:score];
			timeLeft=20;
			[self timeControl:1	];			
		}
		if(M==4) //if mode is 4, set time, life remaining and start timer
		{
			[self nextQ:score];
			life=3;
			timeLeft=20;
			[self timeControl:1	];			
		}
		if(M==5)  //this is for mode 5 which isn't compelete yet, so far it simlart to to mode 1 with the excpetion that you make change from either a 5 or 10 dollar bill
		{
			
			timeLeft=60;
			[timeDisplay setText:[NSString stringWithFormat:@"60"]];
			[self timeControl:1	];
			[self nextQ:score];
	
		}
	}
	
}







-(void)showTime
{
	if((M==1)||(M==3)||(M==5))  //this is for mode 1,3, and 5 it update time until they run out of it
	{
		if(playing==true)
		{	
			if (timeLeft>=1) //if they have timeleft, decrease it by 1 and update the dispaly 
			{
				timeLeft--;
				[timeDisplay setText:[NSString stringWithFormat:@"%.0f", timeLeft]];
			}
			else  //if they are out of time stop the game and display end game missage and update time
			{ 
				playing=false;	
				//[self timeControl:0	];
				[timeDisplay setText:[NSString stringWithFormat:@"0"]];
				[displayField2 setText:[NSString stringWithFormat:@"Your final score is %ld", score]];
				[displayField setText:[NSString stringWithFormat:@"Out of time"]];
				[self delaying:2];
				
			}
		}
	}
	if(M==2)  //this is for mode 2, it increase the time instead of decreasing it. also it increase it by a factor of .1 so the player can see how fast they are to .1 of a second
	{
		if(playing==true)
		{	
			timeLeft=timeLeft+.1;
			[timeDisplay setText:[NSString stringWithFormat:@"%.0f", floor(timeLeft)]];
		}
	}
	if(M==4) //this if is statement is for dealing with mode 4, it similar to mode 1 and 3 but involue changing the "life" varaiable
	{
		if(playing==true) //check if the player is playing currently
		{	
			if (timeLeft>=1) //if they have time
			{
				timeLeft--; //decrease time by 1 and update time
				[timeDisplay setText:[NSString stringWithFormat:@"%.0f", timeLeft]];
			}
			else
			{
				//this is if the player run out of time, they lose a life
				life--; 
				if (life==0) //if player run out of lives, they get the game over message 
				{
					
					playing=false; //set game to end and show score
					[displayField setText:[NSString stringWithFormat:@"Out of time"]];
					[displayField2 setText:[NSString stringWithFormat:@"Your final score is %i",score]];
					[self delaying:2];
				}
				else  //if player get question wrong and still have lives
				{    //display your score and update time
					[displayField2 setText:[NSString stringWithFormat:@"You have %ld life remaining", life]];
					timeLeft=20;
					[timeDisplay setText:[NSString stringWithFormat:@"20"]];
					
					//this get question depending on your current score
					[self nextQ:score];
				}		
			}
		}
	}	
}
//use to go back to the screen for picking modes
-(IBAction)back:(id)sender;
{
	[self dismissModalViewControllerAnimated:YES];
}



- (void)viewDidLoad {
	current = [CoinCount new];
	[super viewDidLoad];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[current release];
	[super dealloc];
}


-init
{
	timeLeft=0;
	score=0;
	playing=false;
	paused=false;
	stoppage=false;
	goNext=false;
	return self;
	
}



@end
